using System;
using FireStarterModeller.BO;
using FireStarterModeller.Logic.Commands;
using FireStarterModeller.Test.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.Logic.Commands
{
    [TestFixture]
    public class TestDeleteClassCommand
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            TestUtilsLogic.SetupFixture();
        }

        [SetUp]
        public void SetupTest()
        {
            TestUtilsLogic.SetupTest();
        }

        //Delete class
        [Test]
        public void Test_Apply()
        {
            //---------------Set up test pack-------------------
            DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
            DMAssembly assembly = dmSolution.DefaultAssembly;
            DMClass dmClass = assembly.CreateClass("Class1");
            assembly.Save();

            ICommandFactory commandFactory = new CommandFactory();

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dmSolution.AllClasses.Count);

            //---------------Execute Test ----------------------
            ICommand command = commandFactory.CreateDeleteClass(dmClass);
            command.Apply();

            //---------------Test Result -----------------------
            Assert.AreEqual(0, assembly.Classes.Count);
            Assert.AreEqual(0, assembly.Classes.MarkedForDeleteBusinessObjects.Count);
            Assert.AreEqual(0, assembly.Classes.RemovedBusinessObjects.Count);
        }

        //Delete class
        [Test]
        public void Test_Revert()
        {
            //---------------Set up test pack-------------------
            DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
            DMAssembly assembly = dmSolution.DefaultAssembly;
            DMClass dmClass = assembly.CreateClass("Class1");
            assembly.Save();

            ICommandFactory commandFactory = new CommandFactory();
            ICommand command = commandFactory.CreateDeleteClass(dmClass);
            command.Apply();

            //---------------Assert Precondition----------------
            Assert.AreEqual(0, assembly.Classes.Count);

            //---------------Execute Test ----------------------
            command.Revert();

            //---------------Test Result -----------------------
            Assert.AreEqual(1, assembly.Classes.Count);
            Assert.AreSame(dmClass, assembly.Classes[0]);
            Assert.IsFalse(dmClass.Status.IsDeleted);
        }

        //Delete class
        [Test]
        [Ignore("This test is failing, have a look at this")] 
        //TODO Mark 16 Mar 2009: Ignored test - "This test is failing, have a look at this"
        public void Test_Revert_Fail()
        {
            //---------------Set up test pack-------------------
            DMSolution dmSolution = TestUtilsShared.CreateSavedDMSolution();
            DMAssembly assembly = dmSolution.DefaultAssembly;
            DMClass dmClass1 = assembly.CreateClass("Class1");
            assembly.Save();

            ICommandFactory commandFactory = new CommandFactory();
            ICommand command = commandFactory.CreateDeleteClass(dmClass1);
            command.Apply();

            assembly.MarkForDelete();
            assembly.Save();

            //---------------Execute Test ----------------------
            try
            {
                command.Revert();
                Assert.Fail("Revert should have failed due to duplicate");
            }

                //---------------Test Result -----------------------
            catch (Exception)
            {
                Assert.AreEqual(0, assembly.Classes.Count);
            }
        }
    }
}